package com.taotao.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.pojo.TbItemCatExample.Criteria;
import com.taotao.service.ItemCatService;

import pojo.EasyUITreeNode;

@Service
public class ItemCatServiceImpl implements ItemCatService {
	@Autowired
	private TbItemCatMapper tbItemCatMapper;
	@Override
	public List<EasyUITreeNode> getCatList(long parentId) {
		//设置查询条件examle
		TbItemCatExample example =new TbItemCatExample();
		Criteria criteria = example.createCriteria();
		//设置具体条件
		criteria.andParentIdEqualTo(parentId);
		//根据parentId查询树形菜单该父节点下的子节点,但是这个list不是我们需要的list
		List<TbItemCat> list = tbItemCatMapper.selectByExample(example);
		//我们需要的list,应该是封装的EasyUITreeNode类
		List<EasyUITreeNode> treeList =new ArrayList<>();
		for (TbItemCat tbItemCat : list) {
			//创建EasyUITreeNode对象
			EasyUITreeNode node =new EasyUITreeNode();
			node.setId(tbItemCat.getId());
			node.setText(tbItemCat.getName());
			//判断是否是叶子节点,使用三元运算符,如果是叶子节点,则 closed
			node.setState(tbItemCat.getIsParent() ? "closed":"open");
			//将EasyUITreeNode对象放在treeList中
			treeList.add(node);
		}
		return treeList;
	}

}
